package org.example.maven.controller;

import org.example.maven.annotation.ReadOnlyMode;
import org.example.maven.pojo.Result;
import org.example.maven.pojo.operation.SearchOperate;

import org.example.maven.service.OperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@CrossOrigin
@RequestMapping("/operate")
public class OperateController {
    @Autowired
    private OperateService operateService;

    @GetMapping
    @PreAuthorize("hasAnyAuthority('log:view')")
    //获取所有操作日志功能
    public Result getOperateList(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
        Result result=Result.success();
        result.put("data",operateService.getOperateList(pageNum,pageSize));
        result.put("total",operateService.getOperateCount(new SearchOperate()));
        return result;
    }

    @GetMapping("/getSpecieOperation")
    @PreAuthorize("hasAnyAuthority('log:view')")
    //获取特定的操作日志
    public Result getSpecieOperation(SearchOperate searchOperate,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        Result result=Result.success("查询成功");
        System.out.println(searchOperate);
        result.put("data",operateService.getSpecieOperation(searchOperate,pageNum,pageSize));
        result.put("total",operateService.getOperateCount(searchOperate));
        return result;
    }

    @DeleteMapping("/{ids}")
    @PreAuthorize("hasAnyAuthority('log:view')")
    //根据id删除操作日志
    @ReadOnlyMode
    public Result deleteOperation(@PathVariable String[] ids){
        System.out.println(Arrays.toString(ids));
        operateService.deleteOperation(ids);
        return Result.success("删除成功");
    }

    @DeleteMapping()
    @PreAuthorize("hasAnyAuthority('log:view')")
    //删除全部操作日志
    @ReadOnlyMode
    public Result deleteOperateAll(){
        operateService.deleteOperationAll();
        return Result.success("删除成功");
    }
}
